//
//  DataMemoryTools.m
//  FoodCorner
//
//  Created by Summer on 16/3/3.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "DataMemoryTools.h"
#import "MainListModel.h"
#import <FMDB/FMDB.h>
@implementation DataMemoryTools

+ (instancetype)shareDataMemoryTools
{
    static DataMemoryTools *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[DataMemoryTools alloc]init];
    });
    return instance;
}

///创建一个数据库的对象
static FMDatabase  *db = nil;
///打开数据库
+ (void)openDataBase
{
    //查找本地存储数据的路径
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    //创建数据库文件
    documentPath = [documentPath stringByAppendingString:@"/Food.sqlite"];
    NSLog(@"%@",documentPath);
    //创建数据库
    db  = [FMDatabase databaseWithPath:documentPath];
    //判断数据库是否被打开
    if ([db open]) {
        NSString *sql = @"CREATE TABLE IF NOT EXISTS FoodDataBase(id text PRIMARY KEY NOT NULL Unique,title text NOT NULL,ingredients text NOT NULL,tags text NOT NULL,burden text NOT NULL,imtro text NOT NULL,steps blob NOT NULL,albums blob NOT NULL)";
       BOOL result = [db executeQuery:sql];
        NSLog(@"数据库打开成功");
        if (result) {
            NSLog(@"创表成功");
        }else
        {
            NSLog(@"创表失败");
        }
    }else
    {
        NSLog(@"数据库打开失败");
    }
    

}
///添加到收藏页面
+ (void)addFavoriteFood:(MainListModel *)model
{
    NSData *steps = [NSKeyedArchiver archivedDataWithRootObject:model.steps];
    NSData *albums = [NSKeyedArchiver archivedDataWithRootObject:model.albums];
    [db executeUpdateWithFormat:@"INSERT INTO FoodDataBase(id,title,ingredients,tags,burden,imtro,steps,albums) VALUES (%@,%@,%@,%@,%@,%@,%@,%@)",model.id,model.title,model.ingredients,model.tags,model.burden,model.imtro,steps,albums];
}
///判断是否包含这一个Model对象，如何存在就返回NO，同时不允许，再次添加数据
+ (BOOL)determineAnyModelIsHas:(MainListModel *)model
{
    FMResultSet *set = [db executeQuery:@"SELECT * FROM FoodDataBase"];
    NSString *name = nil;
    while ([set next]) {
        name = [set stringForColumn:@"title"];
        if ([model.title isEqualToString:name]) {
            return YES;
        }
    }
    return NO;
}
///返回一个数组，轻重数组为数据库中所添加的数据
+ (NSArray *)allOfTheFoodsYouLove
{
    NSMutableArray *tempArray = [NSMutableArray array];
    FMResultSet *set = [db  executeQuery:@"SELECT * FROM FoodDataBase"];
    while ([set next]) {
        MainListModel *model = [[MainListModel alloc]init];
        model.imtro  = [set stringForColumn:@"imtro"];
        model.ingredients = [set stringForColumn:@"ingredients"];
        NSArray *albums = [NSKeyedUnarchiver unarchiveObjectWithData:[set dataForColumn:@"albums"]];
        model.albums = albums;
        NSArray *steps  = [NSKeyedUnarchiver unarchiveObjectWithData:[set dataForColumn:@"steps"]];
        model.steps  = steps;
        model.title  = [set stringForColumn:@"title"];
        model.tags   = [set stringForColumn:@"tags"];
        model.burden = [set stringForColumn:@"burden"];
        model.id = [set stringForColumn:@"id"];

    }
    return tempArray;
}
///删除数据库的所有数据
+ (void)removeAllItemsFromDataBase
{
    [db executeUpdate:@"DELETE * FROM FoodDataBase"];
}
+ (void)deleteItemFromDataBase:(MainListModel *)model
{
    [db executeUpdate:@"DELETE FROM FoodDataBase WHERE title = (%@)",model.title];
}
@end
